package struts.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import struts.form.ActionForm;
import struts.form.FullForm;
import struts.form.XmlBean;

/**
 * 创建者 zhuhong  E-mail: zhuhong719@126.com
 * 创建时间：Jul 16, 2014 8:16:51 PM
 * 版本：
 * 类说明：
 * 修改者：
 * 修改时间：Jul 16, 2014 8:16:51 PM
 * 修改说明：
 */
public class ActionServlet extends HttpServlet{

	/**
	 * 序列化
	 */
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		
	}
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		String path=this.getPath(request.getServletPath());
		Map<String, XmlBean> map=(Map<String, XmlBean>)this.getServletContext().getAttribute("struts");
		XmlBean bean=map.get(path);
		String formclass=bean.getFormClass();
		ActionForm form=FullForm.full(formclass, request);
		String actionClass=bean.getActionClass();
		Action action=null;
		String url="";
		try {
			Class clazz=Class.forName(actionClass);
			action=(Action)clazz.newInstance();
			url=action.execute(form,bean.getActionFormward(),request,response);
		} catch (Exception e) {
			System.out.println("严重：控制器异常！");
		}
		RequestDispatcher dis=request.getRequestDispatcher(url);
		dis.forward(request, response);
	}
	
	/**
	 * 获取请求的规则字符串
	 * @param servletPath
	 * @return
	 */
	private String getPath(String servletPath){
		return servletPath.split("\\.")[0];
	}
}
